/*
 * XLSHandler.java
 *
 * Created on 14 September 2006, 6:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fasea.index.filehandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.lucene.document.Field;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.lucene.document.Document;

/**
 *
 * @author gvasil
 */
class XLSHandler extends FileHandler {
    
    public Document getTypedDocument(File file) throws FileHandlerException {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new FileHandlerException(
                    "File not found: "
                    + file.getAbsolutePath(), e);
        }
        
        org.apache.lucene.document.Document doc = createDefaultLuceneDocument(file);

        String text = null;
        try {
            text = getText(is);
            /* Eat consecutive whitespace characters */
   //         text = text.replaceAll("\\s+", " ");
        }
        catch(IOException e) {
            throw new FileHandlerException("Cannot read the text document", e);
        }

        if (text != null && text.trim().length() > 0) {
            doc.add(new Field("content", text, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.WITH_OFFSETS));
        }
        return doc;
    }
    
    private String getText(InputStream is) throws IOException {
        POIFSFileSystem fs;
        HSSFWorkbook workbook;
        
        fs = new POIFSFileSystem(is);
        workbook = new HSSFWorkbook(fs);

        StringBuilder text = new StringBuilder();
        
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            HSSFSheet sheet = workbook.getSheetAt(i);
            
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                
                Iterator cells = row.cellIterator();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            String num = Double.toString(cell.getNumericCellValue());
                            text.append(num).append(" ");
                            break;
                        case HSSFCell.CELL_TYPE_STRING:
                            String string = cell.getRichStringCellValue().getString();
                            text.append(string).append(" ");
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA:
                            String formula = Double.toString(cell.getNumericCellValue());
                            text.append(formula).append(" ");
                            break;
                        default: // skip
                    }
                }
                text.append("\n");
            }
        }
        return text.lastIndexOf(" \n")==text.length()-2?text.substring(0,text.length()-2):text.toString();
    }
}
